﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections;
using DokuzEylul.Sinav.Entities;
using System.Collections.Generic;
using dokuzeylulworkshop.MasterUtilities;
using Unipa.Framework.Data;
using DokuzEylul.Sinav.enums;
using dokuzeylulworkshop.BaseUtility;

namespace DokuzEylul.Sinav.Utility_Master
{
    public class MenuUtilities
    {
        private static MenuUtilities sm_MenuUtilies;
        public MenuUtilities ()
	    {

	    }
        public static MenuUtilities Instance
        {
            get
            {
                if (sm_MenuUtilies == null)
                    sm_MenuUtilies = new MenuUtilities();
                return sm_MenuUtilies;
            }
        }
        public const string GOSTER_ROLE_CODE = "GOSTER";
        public  TreeView m_treeview 
        {
            get
            {
                
                TreeView _result = HttpContext.Current.Session["Tree_View"] as TreeView;
                if (_result == null)
                    HttpContext.Current.Session["Tree_View"] = _result = new TreeView();
                return _result;
            }
        }
        public  TreeView RenderMenu()
        {
            m_treeview.Nodes.Clear();
            TreeNode node;
            foreach (SinavMenu menuitem in m_SinavMenu)         
            {
                    if (menuitem.MenuType == (int)menutype.menu)
                    {
                        if (MenuYetki(menuitem.MenuId))
                        {
                            node = new TreeNode(menuitem.MenuTitle, "-1");
                            //node.Expanded = false;
                            node.ImageUrl = "~/DE_Resources/images/Menu/menuup.gif";
                            m_treeview.Nodes.Add(node);
                            getChildren(node, menuitem.MenuId);
                        }
                    }
            }
            return m_treeview;
        }
        private  void getChildren(TreeNode node, int menuid)
        {
           
            TreeNode Node = null;
            foreach (SinavMenu _menuitem in m_SinavMenu) 
            {
                if (menuid == _menuitem.MenuParentId )     
                {
                    if (MenuYetki(_menuitem.MenuId))
                    {
                        Node = new TreeNode(_menuitem.MenuTitle);
                        if (_menuitem.MenuType == (int)menutype.menu)
                        {
                            Node.Value = "-1";
                            Node.ImageUrl = "~/DE_Resources/images/Menu/menuup.gif";
                        }
                        else
                        {
                            Node.Value = _menuitem.MenuUrl;
                            Node.NavigateUrl = _menuitem.MenuUrl;
                            Node.ImageUrl = "~/DE_Resources/images/Menu/menudown.gif";
                            
                        }
                        node.ChildNodes.Add(Node);
                        getChildren(Node, _menuitem.MenuId);
                    }
                }
            }
        }
        private  IList<SinavMenu> GetAltMenu(int menuId)
        {
            return m_dbmaster.Select<SinavMenu>("select * from sinavmenu where menuparentId = ?", menuId);
        }
        private  IList<SinavUserRole> m_CurrentUserRoles
        {
            get
            {
                IList<SinavUserRole> _result = HttpContext.Current.Session["Current_User_Roles"] as IList<SinavUserRole>;
                if (_result != null)
                    return _result;
                else
                    throw new Exception("Yetkileri belirlenmiş bir kullanıcı bulunamadı!");
            }
        }
        private  bool IsAuthorized(int menuid, string rolecode)
        {
            foreach (SinavUserRole _result in m_CurrentUserRoles)
            {
                if (_result.MenuId == menuid && _result.RoleCode == rolecode)
                {
                    return true;
                }
            }
            return false;
        }
        private  IList<SinavMenu> m_SinavMenu
        {
            get
            {
                IList<SinavMenu> _result = HttpContext.Current.Session["Sinav_Menu"] as IList<SinavMenu>;
                if (_result == null)
                    HttpContext.Current.Session["Sinav_Menu"] = _result = GetMenuList();
                return _result;
            }
        }
        private  HybridDatabase m_dbmaster
        {
            get
            {
                return dbmaster.Instance.dbfactory;
            }
        }
        private  bool MenuYetki(int _menuID)
        {
            if (m_CurrentUserRoles != null && m_CurrentUserRoles.Count != 0)
                foreach (SinavUserRole _role in m_CurrentUserRoles)
                {
                    if (_role.MenuId == -1)
                        return true;
                    if (_role.MenuId == _menuID)
                        return true;
                }
            return false;
        }

        /// <summary>
        /// Tüm menu listesini getirir
        /// </summary>
        /// <returns></returns>
        public  IList<SinavMenu> GetMenuList()
        {
           return m_dbmaster.Select<SinavMenu>("select * from SinavMenu");
        }
        /// <summary>
        /// en üstte yer alan menuleri getirir
        /// </summary>
        /// <returns></returns>
        public  IList<SinavMenu> GetParentList()
        {
            return m_dbmaster.Select<SinavMenu>("select * from SinavMenu where MenuType=0");
        }
        /// <summary>
        /// ID si verilmiş menu bilgilerini getirir
        /// </summary>
        /// <param name="MenuId"></param>
        /// <returns></returns>
        public  SinavMenu GetMenu(int MenuId)
        {
            return m_dbmaster.SelectSingle<SinavMenu>("select * from SinavMenu where MenuId=?", MenuId);
        }
        /// <summary>
        /// ilgili menu elemanının database den silinmesini saglar;
        /// </summary>
        /// <param name="_menu"></param>
        public  void DeleteMenu(SinavMenu _menu)
        {
            //TODO - alt menulerin olup olmadıgı kontrol edilir
            m_dbmaster.DeleteDbEntity(_menu);
        }
       

          
    }
}
